package com.example.pojo;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/*
 *  @author 雨安
 *  类名： LoginUser
 *  创建时间：2024/3/18
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class LoginUser implements UserDetails {

    //自己的users对象
    private Users user;

    //权限
    private List<String> permissions;

    //带参构造方法
    public LoginUser(Users user, List<String> permissions) {
        this.user = user;
        this.permissions = permissions;
    }

    //他需要序列化继承了所以不需要存入Redis中,所以不会进行序列化到redis当中
    @JSONField(serialize = false)
    //封装成变量
    private List<SimpleGrantedAuthority> authorities;


    //返回权限信息
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (authorities != null)
            return authorities;
        else{
            //创建流
            authorities = permissions.stream()
                    .map(SimpleGrantedAuthority::new)
                    .collect(Collectors.toList());//返回权限信息
            //返回数据
            return authorities;
        }

        //        List<GrantedAuthority> newList = new ArrayList<>();
//        //把permissions中String类型的权限信息封装成SimpleGrantedAuthority对象
//        for (String permission : permissions) {
//            //拿到集合泛型要求的对象
//            SimpleGrantedAuthority authority
//                    = new SimpleGrantedAuthority(permission);
//            //将数据添加到集合中
//            newList.add(authority);
//        }

    }

    //获取密码
    @Override
    public String getPassword() {
        return user.getPassword();
    }

    //获取账号
    @Override
    public String getUsername() {
        return user.getUserName();
    }

    //判断没过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    //是否
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    //是否超时
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    //是否可用
    @Override
    public boolean isEnabled() {
        return true;
    }
}
